package com.bobbygu.server.simpleapp.controller;

import com.bobbygu.server.simpleapp.model.response.BaseResponse;
import com.bobbygu.server.simpleapp.model.response.ConstResponse;
import com.bobbygu.server.simpleapp.model.response.ResultResponse;
import com.bobbygu.server.simpleapp.socket.ConnectionServer;
import com.bobbygu.server.simpleapp.socket.protocol.DataAckProtocol;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

@RestController
public class PushMsgController {

    @RequestMapping(value = "/home/PushMsg.do")
    public BaseResponse PushMsg(@RequestBody Map<String, Object> requestMap) {
        System.out.println("PushMsg.do");
        if (requestMap.get("Name") == null) {
            return new BaseResponse("Name 不能为空", ConstResponse.STATUS_PARAM_ERROR);
        }
        if (requestMap.get("Msg") == null) {
            return new BaseResponse("Msg 不能为空", ConstResponse.STATUS_PARAM_ERROR);
        }
        String name = requestMap.get("Name").toString();
        String msg = requestMap.get("Msg").toString();
        if (name != null) {
//            homeMapper.deleteImgByName(name);
            DataAckProtocol dataAckProtocol = new DataAckProtocol();
            dataAckProtocol.setUnused(msg);
            ConnectionServer.pushMsg(name, dataAckProtocol);
            return new BaseResponse(ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
        } else {
            return new BaseResponse(ConstResponse.DESC_PARAM, ConstResponse.STATUS_PARAM_ERROR);
        }
    }

    @RequestMapping(value = "/home/GetClientList.do")
    public ResultResponse getClientList() {
        ArrayList<String> list = ConnectionServer.getClientList();
        return new ResultResponse(list, ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
    }
}
